<?php

include_once "variables.php";

//database functions

function selectQuery($query)
{
	$retVal=null;
	global $message;
	global $successMessage;
	
	try {
		if (empty($query))
		{
			$GLOBALS['message'] ="Please provide a select query to be executed.";
			throw new Exception ($GLOBALS['message'] );
		}
		else
		{
			
			 try 	 
			 {				
					if (!$link = mysql_connect($GLOBALS['databaseServer'], $GLOBALS['userName'], $GLOBALS['Password'])) {
						$GLOBALS['message']  = 'An Error Occurred: Could not connect to the database. Please try again. '.mysql_error();
						throw new Exception ($GLOBALS['message'] );
					}
					
					
					if (!mysql_select_db($GLOBALS['database'], $link)) {
						$GLOBALS['message']  = 'An Error Occurred: Could not connect to the database. Please try again. '.mysql_error();
						throw new Exception ($GLOBALS['message'] );
					}					
					
					$result = mysql_query($query, $link);
					

					if (!$result) {
						$GLOBALS['message']  =  "DB Error, could not query the database. ".mysql_error();
						throw new Exception ($GLOBALS['message'] );
					}
					
					$num_rows = mysql_num_rows($result);
					
					if ($num_rows>0)
					{
						$retVal =  array();
						
						while ($row = mysql_fetch_assoc($result))
						{
							array_push($retVal, $row);
						}
						
						mysql_free_result($result);
					}
					else
					{
						mysql_free_result($result);
						return null;
					}
					
			  }//end oi try
			  catch (Exception $e)
			  {
					return null;
			  }	
				
				return $retVal;
		}
	}//end of try
	catch (Exception $ex)
	{
		return null;
	}	
}

/*This function executes an insert/update/delete query*/
function executeQuery($query)
{
	$retVal=null;
	//global $message;
	//global $successMessage;
	
	try {
		if (empty($query))
		{
			$GLOBALS['message'] ="Please provide a select query to be executed.";
			throw new Exception ($GLOBALS['message'] );
		}
		else
		{
			
			 try 	 
			 {
					if (!$link = mysql_connect($GLOBALS['databaseServer'], $GLOBALS['userName'], $GLOBALS['Password'])) {
						$GLOBALS['message']  = 'An Error Occurred: Could not connect to the database. Please try again. '.mysql_error();
						throw new Exception ($GLOBALS['message'] );
					}
							
					if (!mysql_select_db($GLOBALS['database'], $link)) {
						$GLOBALS['message']  = 'An Error Occurred: Could not connect to the database. Please try again. '.mysql_error();
						throw new Exception ($GLOBALS['message'] );
					}					
					
					$result = mysql_query($query, $link);
					
					if (!$result) {
						$GLOBALS['message']  =  "DB Error, could not query the database. ".mysql_error();
						throw new Exception ($GLOBALS['message'] );
					}					
					
					$num_rows = mysql_affected_rows();
					
					return $num_rows;
					
			  }//end of try
			  catch (Exception $e)
			  {
					return null;
			  }	
					
				return $retVal;
		}
	}//end of try
	catch (Exception $ex)
	{
		return null;
	}	
}


function test ()
{

    $result = "";
    for ($i = 0;$i < func_num_args();$i++) {
      $result .= func_get_arg($i) . " ";
    }
	
	echo '<br/>'.$result.'<br/>';
}


///////////////////////////


/*This function executes a sql insert/update/delete query
parameters are as follows
@param1 - query
@param2 - string for variables s-string, i-integer, b-blob, d-double
@param3- various arguments

*/
/*
function executeQuery()
{
			$retVal=null;
			global $message;
			global $successMessage;
			
			$args =  array();
			
//getting the arguments
    for ($i = 0;$i < func_num_args();$i++) {
		$var = func_get_arg($i);
      array_push($args,$var);
    }			
			
	try {		
			if (count($args)<3)
			{
				$message="You have provided invalid input for this function.";
				throw new Exception ($message);
			}
			else
			{			
				try 
				{
					//getting the variables
					$query = $args[0];
					$varString = $args[1];
					
					//building the rest of the bind_param string
					$paramString='';
					
					for ($i=2;$i<count($args);$i++)
					{
						$varName = 'var'.$i;
						$$varName = $args[$i];
						//$paramString = $paramString."'".$args[$i]."',";
						$paramString = $paramString."\$".$varName.",";
					}
					$paramString = substr($paramString,0,-1);
					
					$bindParamArgs = "'".$varString."',".$paramString;
					
					//echo $bindParamArgs ;
					
					//exit;
				
					@$db = new mysqli ($GLOBALS['databaseServer'], $GLOBALS['username'], $GLOBALS['password'], $GLOBALS['database']);
					
					if (mysqli_connect_errno())
					{
						$message = 'An Error Occurred: Could not connect to the database. Please try again. '.mysqli_connect_error();
						throw new Exception ($message);
					}
					
		
					if (!$stmt = @$db->prepare($query))
					{
						$message= 'An Error Occurred while attemping to initiate the query : '.$stmt->error;
						throw new Exception ($message);
					}
					
					echo $query.'<br>'.$bindParamArgs.'<br/>';
					//exit;
					
					//$stmt->bind_param(eval($bindParamArgs));

					//if (mysqli_connect_errno())
					//{
					//	$message = 'An Error Occurred: while attempting to bind parameters - '.mysqli_connect_error();
					//	throw new Exception ($message);
					//}					
					//
					
					if (!$result=$stmt->bind_param(eval(eval($bindParamArgs))))
					{
						$message = 'An Error Occurred: while attempting to bind parameters - '.$stmt->error;
						throw new Exception ($message);
					}								
					
					// execute prepared statement 
					if (!$stmt->execute())
					{
						$message ='An error occured : '. $stmt->error;
						$db->close(); 	
						throw new Exception ($message);			
					}
					
					$retVal= $stmt->affected_rows;
					
					 //close statement and connection 
					$stmt->close();			
					
					//close connection 
					$db->close(); 			
					
		
				}
				catch (Exception $e)
				{
					return null;
				}
				
				return retVal;
					
			} //end of else
		}//end of try
		catch (Exception $ex)
		{
			return null;
		}
}
*/
/////////////////////////////

//dates come in the format mm/dd/yyyy
//dates are returned in the format yyyy/mm/dd

function formatDateString($dateIn)
{
	if (empty($dateIn))
	{
		return null;
	}	
		
	if (ereg( "([0-9]{1,2})/([0-9]{1,2})/([0-9]{4})",$dateIn, $args  ))
	{
		$month = $args[1];
		$day = $args[2];
		$year = $args[3];
		
		if (strlen(trim($month))<2) {$month = "0".$month;}
		if (strlen(trim($day))<2) {$day= "0".$day;}
		if (strlen(trim($year))<2) {$month = "0".$year;}
		
		$retVal = $year."/".$month."/".$day;

		return $retVal;

	}
	else
	{
		return "error";
	}	

}


//Get the Visitor Count for the site
function getCount()
{
	$retVal=0;
  /*
	$sql = "Select VisitorCount from Counter where ID=1";
	$results = selectQuery($sql);
	
	if( (!empty($results)) && (sizeof($results)==1))
	{
		foreach($results as $row)
		{
			$retVal = $row['VisitorCount'];
		}
	}
	*/
	return $retVal;

}

//Increase the visitor counter for the site
function increaseCount()
{
	/*
	$sql = "Update Counter set VisitorCount = VisitorCount + 1 where ID=1";
	$num_Results = executeQuery($sql);
*/
}


?>